package com.neu.admin;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.neu.doc.AdminRegionController_list;
import com.neu.domain.ShoppingRegion;
import com.neu.service.ShoppingRegionService;
import com.neu.util.ResponseUtil;
import com.neu.vo.RegionVo;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 地域信息
 * @author nickL
 *
 */
@CrossOrigin
@RestController
@RequestMapping("/admin/region")
@Validated
public class AdminRegionController {
    private final Log logger = LogFactory.getLog(AdminRegionController.class);
@Autowired
private ShoppingRegionService shoppingRegionService;
@GetMapping("/list")
public Object list(){
    List<RegionVo> regionVoList = new ArrayList<>();
//    先传入一级分类省信息 查询一级分类结果
    List<ShoppingRegion> provinceList = shoppingRegionService.queryByPid(0);
    for (ShoppingRegion province : provinceList) {
        RegionVo provinceVO = new RegionVo();
        provinceVO.setId(province.getId());
        provinceVO.setName(province.getName());
        provinceVO.setCode(province.getCode());
        provinceVO.setType(province.getType());
//        根据省份信息查询市信息 二级分类
        List<ShoppingRegion> cityList = shoppingRegionService.queryByPid(province.getId());
        List<RegionVo> cityVOList = new ArrayList<>();
        for (ShoppingRegion city : cityList) {
            RegionVo cityVO = new RegionVo();
            cityVO.setId(city.getId());
            cityVO.setName(city.getName());
            cityVO.setCode(city.getCode());
            cityVO.setType(city.getType());
//          根据市信息查询区信息 三级分类
            List<ShoppingRegion> areaList = shoppingRegionService.queryByPid(city.getId());
            List<RegionVo> areaVOList = new ArrayList<>();
            for (ShoppingRegion area : areaList) {
                RegionVo areaVO = new RegionVo();
                areaVO.setId(area.getId());
                areaVO.setName(area.getName());
                areaVO.setCode(area.getCode());
                areaVO.setType(area.getType());
                areaVOList.add(areaVO);
            }
//            市信息中添加对应的区信息
            cityVO.setChildren(areaVOList);
            cityVOList.add(cityVO);
        }
//        省份信息加入对应的市信息{{省,children{市，children{区}}}
        provinceVO.setChildren(cityVOList);
        regionVoList.add(provinceVO);
    }

    return ResponseUtil.okList(regionVoList);
}

   
}
